package com.yiquan.chat.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.yiquan.chat.pojo.Message;
import com.yiquan.chat.service.MessageService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;

/**
 * 控制器层
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private MessageService messageService;


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", messageService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable Long id) {
        return new Result(true, StatusCode.OK, "查询成功", messageService.findById(id));
    }


    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Message> pageList = messageService.findSearch(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Message>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(true, StatusCode.OK, "查询成功", messageService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param message
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Message message) {
        messageService.add(message);
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param message
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Message message, @PathVariable Long id) {
        message.setId(id);
        messageService.update(message);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable Long id) {
        messageService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 根据会话id查询聊天记录
     *
     * @param converseId
     * @return
     */
    @GetMapping("/getMsgByConverseId")
    public Result getMsgByConverseId(@RequestParam Long converseId, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int size) {
        Page<Message> messageList = messageService.getMsgByConverseId(Long.valueOf(converseId), page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Message>(messageList.getTotalElements(), messageList.getContent()));
    }

    /**
     * 根据会话id获取未读信息
     *
     * @param converseId
     * @return
     */
    @GetMapping("/getUnReadMsgByConverseId")
    public Result getUnReadMsgByConverseId(@RequestParam Long converseId) {
        List<Message> messageList = messageService.getUnReadMsgByConverseId(Long.valueOf(converseId));
        return new Result(true, StatusCode.OK, "查询成功", messageList);
    }

    /**
     * 根据会话id获取未读信息数量
     * @param converseId
     * @return
     */
    @GetMapping("/countUnReadMsgByConverseId")
    public Result countUnReadMsgByConverseId(@RequestParam Long converseId) {
        Integer count = messageService.countUnReadMsgByConverseId(Long.valueOf(converseId));
        return new Result(true, StatusCode.OK, "查询成功", count);
    }

    @GetMapping("/updateUnreadMsg")
    public Result updateUnreadMsg(@RequestParam Long converseId){
        messageService.readUnReadMsgByConverseId(converseId);
        return new Result(StatusCode.REQUEST_SUCCESS);
    }

}
